home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
filesyst
/
dosfs
/
dmsdosfs.000
/
dmsdosfs
/
dmsdosfs-0.6.9b
/
daemon_actions.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-06-30
|
4KB
|
195 lines
/*
daemon_actions.c
DMSDOS filesystem: dmsdos daemon
The DMSDOS filesystem was written by Frank Gockel
(gockel@etecs4.uni-duisburg.de).
COPYRIGHT (C) 1995,1996 Frank Gockel
Distributed under the GNU General Public License.
*/
#include<stdio.h>
#include<linux/dmsdos_fs.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include<unistd.h>
/* default compression level minus 1 for dmsdosd */
int cfaktor=11;
int debug=0;
int exit_signaled=0;
#define CONTINUE 0
#define EXIT_AND_FINISH 1
#define EXIT_IMMEDIATELY 2
typedef struct
{ long val1;
long val2;
long val3;
unsigned char data[32*1024];
} Cdata;
Cdata cdata;
Cdata ckdata;
void signal_handler(int a)
{ if(a==SIGINT)exit_signaled=EXIT_AND_FINISH;
if(a==SIGTERM)exit_signaled=EXIT_IMMEDIATELY;
}
int get_and_compress_one(int fd)
{ int ret;
int handle;
int length;
int size;
int method;
/* get cluster to compress */
if(exit_signaled!=CONTINUE)return 2;
if(debug)printf("dmsdosd: Trying to read...\n");
ret=ioctl(fd,DMSDOS_D_READ,&cdata);
if(ret!=1)
{ if(debug)printf("dmsdosd: nothing there - D_READ ioctl returned %d\n",ret);
return ret;
}
handle=cdata.val1;
length=cdata.val2;
size=(length-1)/512+1;
method=cdata.val3;
if(debug)printf("dmsdosd: compressing...\n");
ret=compress(ckdata.data,cdata.data,size,method,0);
if(debug)printf("dmsdosd: compress returned %d\n",ret);
if(ret<0)ret=0; /* compression failed */
ckdata.val1=handle;
ckdata.val2=ret;
if(debug)printf("dmsdosd: writing...\n");
ioctl(fd,DMSDOS_D_WRITE,&ckdata);
return 1; /* one cluster compressed */
}
void do_dmsdosd_actions(int fd)
{
/* register dmsdosd */
if(debug)printf("dmsdosd: calling D_ASK...\n");
if(ioctl(fd,DMSDOS_D_ASK,NULL))
{ printf("dmsdosd: can't get permissions. Try again as root.\n");
return;
}
signal(SIGINT,signal_handler);
signal(SIGTERM,signal_handler);
do
{ while(get_and_compress_one(fd)==1);
/* don't kill the system performance when nothing to compress */
if(exit_signaled==CONTINUE)
{ if(debug)printf("dmsdosd: sleeping...\n");
sleep(30); /* 30secs is the bdflush default */
}
}
while(exit_signaled==CONTINUE);
if(debug)printf("dmsdosd: calling D_EXIT...\n");
ioctl(fd,DMSDOS_D_EXIT,NULL);
if(exit_signaled==EXIT_AND_FINISH)
{ exit_signaled=CONTINUE;
while(get_and_compress_one(fd)==1);
}
}
void panic(char*text)
{ printf("dmsdosd panic: %s\n",text);
abort();
}
int scan(char*arg)
{ int w;
if(strncmp(arg,"0x",2)==0)sscanf(arg+2,"%x",&w);
else sscanf(arg,"%d",&w);
return w;
}
void error(int err)
{ if(err==-EPERM)printf("Permission denied.\n");
else if(err==-EIO)printf("I/O Error.\n");
else if(err==-EINVAL)printf("Invalid parameter.\n");
else printf("Error %d occured.\n",err);
}
void main(int argc, char*argv[])
{ Dblsb dblsb;
int fd;
int ret;
if(argc<2||argc>4)
{
printf("DMSDOS daemon (C) 1996 Frank Gockel\n");
printf("Usage: %s (directory)\n",argv[0]);
printf(" %s (directory) cf\n",argv[0]);
printf(" %s (directory) cf debug\n",argv[0]);
return;
}
if(argc==4)debug=1;
fd=open(argv[1],O_RDONLY);
if(fd<0)
{ printf("No such file or directory.\n");
return;
}
if(argc==3)
{ cfaktor=scan(argv[2])-1;
if(cfaktor<0||cfaktor>11)
{ printf("parameter out of range\n");
close(fd);
return;
}
}
/* this hack enables reverse version check */
dblsb.s_dcluster=DMSDOS_VERSION;
ret=ioctl(fd,DMSDOS_GET_DBLSB,&dblsb);
if(ret<0)
{ printf("This is not a DMSDOS directory.\n");
close(fd);
return;
}
if(debug)printf("You are running DMSDOS Version %d.%d.%d.\n\n",(ret&0xff0000)>>16,
(ret&0x00ff00)>>8,ret&0xff);
if(ret&0x0f000000)
{ printf("Sorry, this program is too old for the actual dmsdos version.\n");
close(fd);
return;
}
if(ret<0x00000607)
{ printf("This program requires at least DMSDOS Version 0.6.7.\n");
close(fd);
return;
}
do_dmsdosd_actions(fd);
close(fd);
}